package com.easybuy.sys.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easybuy.sys.model.SysStoreMessage;
import com.easybuy.sys.service.SysStoreMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author SR.Y
 * @since 2020-12-05
 */
@RestController
@RequestMapping("/model/sysStoreMessage")
public class SysStoreMessageController {

    @Value("${file.path}")
    public String filePath;
    @Value("${file.url}")
    public String fileUrl;
    @Autowired
    private SysStoreMessageService sysStoreMessageService;

    /**
     * 审核提交
     */
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public void create(@RequestBody SysStoreMessage storeMessage) {
        sysStoreMessageService.create(storeMessage);
    }

    /**
     * 上传图片
     *
     * @param file
     * @param request
     * @return
     * @throws FileNotFoundException
     * @throws IOException
     */
//    @PostMapping("/upload1")
//    public String file(@RequestParam("file") CommonsMultipartFile file, HttpServletRequest request) throws FileNotFoundException, IOException {
//        //获取文件名
//        String filename = file.getOriginalFilename();
//        //获取上传文件的路径
//        String pathString = request.getRealPath("/fileupload");
//        InputStream is = file.getInputStream();
//        OutputStream os = new FileOutputStream(new File(pathString, filename));
//        int len = 0;
//        byte[] bu = new byte[1042];
//        len = is.read(bu);
//        while (len != -1) {
//            os.write(bu, 0, len);
//            len = is.read(bu);
//        }
//        os.close();
//        is.close();
////        System.out.println(filename+"=="+pathString);
//        return pathString + "\\" + filename;
//    }

    @PostMapping("/upload")
    public String upload1(@RequestParam("file") MultipartFile file) {

        //获取上传文件名,包含后缀
        String originalFilename = file.getOriginalFilename();
        //获取后缀
        assert originalFilename != null;
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        //保存的文件名
        String finalFilename = UUID.randomUUID() + suffix;
        //生成保存文件
        File uploadFile = new File(filePath + finalFilename);
        //将上传文件保存到路径
        try {
            file.transferTo(uploadFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return fileUrl + finalFilename;
//      return new SysResponse().code("200").message("上传成功").status("success").data(fileUrl + finalFilename);
    }

    /**
     * 查询时候通过审核
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public Page<SysStoreMessage> list(@RequestParam Integer state,
                                      @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                      @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        return sysStoreMessageService.list(state, pageSize, pageNum);
    }

    /**
     * 修改审核
     */
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public boolean update(@PathVariable Integer id, @RequestBody SysStoreMessage storeMessage) {
        storeMessage.setId(id);
        return sysStoreMessageService.update(storeMessage);
    }

}

